package com.finance.rule.request;


import com.finance.base.constant.PatternConstants;
import com.finance.rule.export.NewPieceRuleImportTemplateBean;
import com.finance.system.service.ICarrierService;
import com.finance.system.service.IRegionCodeService;
import com.finance.system.service.ISupplierService;
import lombok.Data;
import org.apache.commons.lang.StringUtils;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;

import javax.validation.constraints.Pattern;

@Data
public class NewPieceRuleImportReq {

    @NotBlank(message = "规则模式不可为空或者不能有误")
    private String patternRule;

    @NotBlank(message = "供应商名称不可为空或者不能有误")
    private String supplierCode;

    //@NotBlank(message = "承运商名称不可为空或者不能有误")
    private String  carrierCode;

    @NotBlank(message = "目的地省份不可为空或者不能有误")
    private String toProvince;

    @NotBlank(message = "目的市不能有误")
    private String toCityError;
    private Integer toCity;

    //发货省份
    private Integer fromProvince;

    //发货城市
    private Integer fromCity;

    private String regionId;

    //"结算类型"
    private Integer settlementType;

    @Length(max = 11,message = "首件价格不能超过11位")
    @Pattern(regexp = PatternConstants.PATTERN_NUMBER_TWO_DECIMAL_POINT,message = "首件价格最多两位小数")
    private  String firstPiecePrice;

    @Length(max = 11,message = "拖件价格不能超过11位")
    @Pattern(regexp = PatternConstants.PATTERN_NUMBER_TWO_DECIMAL_POINT,message = "拖件价格最多两位小数")
    private String secondPrice;

    @Length(max = 11,message = "代收金额首件不能超过11位")
    @Pattern(regexp = PatternConstants.PATTERN_NUMBER_TWO_DECIMAL_POINT,message = "代收金额首件最多两位小数")
    private String codMajor;

    @Length(max = 11,message = "代收金额拖件不能超过11位")
    @Pattern(regexp = PatternConstants.PATTERN_NUMBER_TWO_DECIMAL_POINT,message = "代收金额拖件最多两位小数")
    private String codDrag;

    @Length(max = 11,message = "配送类型首件不能超过11位")
    @Pattern(regexp = PatternConstants.PATTERN_NUMBER_TWO_DECIMAL_POINT,message = "配送类型首件最多两位小数")
    private String directionMajor;

    @Length(max = 11,message = "配送类型拖件不能超过11位")
    @Pattern(regexp = PatternConstants.PATTERN_NUMBER_TWO_DECIMAL_POINT,message = "配送类型拖件最多两位小数")
    private String directionDrag;

    @Length(max = 11,message = "时效要求首件不能超过11位")
    @Pattern(regexp = PatternConstants.PATTERN_NUMBER_TWO_DECIMAL_POINT,message = "时效要求首件最多两位小数")
    private String expressScheduleTypeMajor;

    @Length(max = 11,message = "时效要求拖件不能超过11位")
    @Pattern(regexp = PatternConstants.PATTERN_NUMBER_TWO_DECIMAL_POINT,message = "时效要求拖件最多两位小数")
    private String expressScheduleTypeDrag;

    @Length(max = 11,message = "温度级别首件不能超过11位")
    @Pattern(regexp = PatternConstants.PATTERN_NUMBER_TWO_DECIMAL_POINT,message = "温度级别首件最多两位小数")
    private String temperatureMajor;

    @Length(max = 11,message = "温度级别拖件不能超过11位")
    @Pattern(regexp = PatternConstants.PATTERN_NUMBER_TWO_DECIMAL_POINT,message = "温度级别拖件最多两位小数")
    private String temperatureDrag;

    @Length(max = 11,message = "重量不能超过11位")
    @Pattern(regexp = PatternConstants.PATTERN_NUMBER_TWO_DECIMAL_POINT,message = "重量最多两位小数")
    private String weight;

    @Length(max = 11,message = "重量首件不能超过11位")
    @Pattern(regexp = PatternConstants.PATTERN_NUMBER_TWO_DECIMAL_POINT,message = "重量首件最多两位小数")
    private String weightMajor;

    @Length(max = 11,message = "重量拖件不能超过11位")
    @Pattern(regexp = PatternConstants.PATTERN_NUMBER_TWO_DECIMAL_POINT,message = "重量拖件最多两位小数")
    private String weightDrag;

    @Length(max = 11,message = "菜鸟驿站首件不能超过11位")
    @Pattern(regexp = PatternConstants.PATTERN_DECIMAIL,message = "菜鸟驿站首件金额格式错误,最多两位小数")
    private String cainiaoMajor;

    @Length(max = 11,message = "菜鸟驿站拖件不能超过11位")
    @Pattern(regexp = PatternConstants.PATTERN_DECIMAIL,message = "菜鸟驿站拖件金额格式错误,最多两位小数")
    private String cainiaoDrag;

    public NewPieceRuleImportReq(){}

    public NewPieceRuleImportReq(NewPieceRuleImportTemplateBean bean,ISupplierService supplierService,ICarrierService carrierService,IRegionCodeService regionCodeService){

        this.setPatternRule(bean.getPatternRuleImport());

        if(StringUtils.isNotBlank(bean.getSupplierCodeImport())){
            String supplierCode = supplierService.getCodeByName(bean.getSupplierCodeImport().trim());
            if(StringUtils.isNotBlank(supplierCode)) {
                this.setSupplierCode(supplierCode);
            }
        }
        if(StringUtils.isNotBlank(bean.getCarrierCodeImport())){
            String carrierCode =  carrierService.getCodeByName(bean.getCarrierCodeImport().trim());
            if(null != carrierCode) {
                this.setCarrierCode(carrierCode);
            }
        }
        if(StringUtils.isNotBlank(bean.getToProvinceImport())){
            String toProvince = regionCodeService.getCodeByName(bean.getToProvinceImport().trim());
            if(StringUtils.isNotBlank(toProvince)) {
                this.setToProvince(toProvince);
            }
            if(StringUtils.isNotBlank(bean.getToCityImport())){
                String toCity = regionCodeService.getCodeByNameAndParent(bean.getToCityImport().trim(),toProvince);
                if(StringUtils.isNotBlank(toCity)) {
                    this.setToCity(Integer.valueOf(toCity));
                    this.setToCityError("false");
                }
            } else {
                this.setToCityError("false");
            }
        }
        if(StringUtils.isNotBlank(bean.getFromProvinceImport())){
            String fromProvinceCode = regionCodeService.getCodeByName(bean.getFromProvinceImport().trim());
            if(StringUtils.isNotBlank(fromProvinceCode)){
                this.setFromProvince(Integer.valueOf(fromProvinceCode));
            }
        }

        if(StringUtils.isNotBlank(bean.getFromCityImport())){
            String fromProvinceCode = String.valueOf(this.getFromProvince());
            String fromCityCode = regionCodeService.getCodeByNameAndParent(bean.getFromCityImport().trim(),fromProvinceCode);
            if(StringUtils.isNotBlank(fromCityCode)){
                this.setFromCity(Integer.valueOf(fromCityCode));
            }
        }
        if(StringUtils.isNotBlank(bean.getRegionIdImport())){
            String level = bean.getRegionIdImport().trim();
            if("一类区域".equals(level) || "1".equals(level)){
                this.setRegionId("0");
            }else if("二类区域".equals(level) || "2".equals(level)){
                this.setRegionId("1");
            }else if("三类区域".equals(level) || "3".equals(level)){
                this.setRegionId("2");
            }
        }
        if(StringUtils.isNotBlank(bean.getSettlementTypeImport())){
            String level = bean.getSettlementTypeImport().trim();
            if("投递员价格".equals(level) || "投递员价".equals(level)){
                this.setSettlementType(Integer.valueOf("0"));
            }else if("合作商价格".equals(level) || "合作商价".equals(level)){
                this.setSettlementType(Integer.valueOf("1"));
            }else if("合作站点价格".equals(level) || "合作站点价".equals(level)){
                this.setSettlementType(Integer.valueOf("2"));
            }
        }
        this.setFirstPiecePrice(StringUtils.isNotBlank(bean.getFirstPiecePrice()) ? bean.getFirstPiecePrice() :"0");
        this.setSecondPrice(StringUtils.isNotBlank(bean.getSecondPrice()) ? bean.getSecondPrice() :"0");

        this.setCodMajor(StringUtils.isNotBlank(bean.getCodMajor()) ? bean.getCodMajor() :"0");
        this.setDirectionMajor(StringUtils.isNotBlank(bean.getDirectionMajor()) ? bean.getDirectionMajor():"0");
        this.setExpressScheduleTypeMajor(StringUtils.isNotBlank(bean.getExpressScheduleTypeMajor()) ? bean.getExpressScheduleTypeMajor():"0");
        this.setTemperatureMajor(StringUtils.isNotBlank(bean.getTemperatureMajor()) ? bean.getTemperatureMajor():"0");
        this.setCodDrag(StringUtils.isNotBlank(bean.getCodDrag()) ? bean.getCodDrag() :"0");
        this.setDirectionDrag(StringUtils.isNotBlank(bean.getDirectionDrag()) ? bean.getDirectionDrag():"0");
        this.setExpressScheduleTypeDrag(StringUtils.isNotBlank(bean.getExpressScheduleTypeDrag()) ? bean.getExpressScheduleTypeDrag():"0");
        this.setTemperatureDrag(StringUtils.isNotBlank(bean.getTemperatureDrag()) ? bean.getTemperatureDrag():"0");
        this.setCainiaoMajor(StringUtils.isNotBlank(bean.getCainiaoMajor()) ? bean.getCainiaoMajor():"0");
        this.setCainiaoDrag(StringUtils.isNotBlank(bean.getCainiaoDrag()) ? bean.getCainiaoDrag():"0");
        this.setWeight(StringUtils.isNotBlank(bean.getWeight()) ? bean.getWeight():"0");
        this.setWeightMajor(StringUtils.isNotBlank(bean.getWeightMajor()) ? bean.getWeightMajor():"0");
        this.setWeightDrag(StringUtils.isNotBlank(bean.getWeightDrag()) ? bean.getWeightDrag():"0");
    }
}